﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace OtherDevice
{
    public partial class Form_ElectricCollimator : Form_ControlBase
    {
        public Form_ElectricCollimator()
        {
            InitializeComponent();
        }
        Collimator_Base Coll;

        public delegate void SaveDistance(double distance);
        public static SaveDistance saveDis;
        public delegate void SaveLight(int ch, int val, string type);
        public static SaveLight saveVal;


      
        public override void FlushToDlg(object devicebase)
        {
            try
            {
                if (devicebase == null)
                    return;
               
                this.Coll = devicebase as Electric_CollimatorCtrl;
                this.Dock = DockStyle.Fill;
                this.Show();
            }
            catch (Exception)
            {

            }

        }
        private async void button_connect_Click(object sender, EventArgs e)
        {
            await Task.Run(() => {
                try
                {
                    string Err = "";
                }
                catch (Exception EX)
                {
                    MessageBox.Show(EX.Message + EX.StackTrace);
                }
            });
        }
        private object a = new object();

        private async void button_Homing_Click(object sender, EventArgs e)
        {
            await Task.Run(() => {
                lock (a)
                {
                    try
                    {
                        string Err = "";
                        Coll.Home(ref Err, false);
                    }
                    catch (Exception EX)
                    {
                        MessageBox.Show(EX.Message + EX.StackTrace);
                    }
                }
            });
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Coll.isConnect)
            {
                button_connect.Text = "断开";
                for (int i = 1; i < 20; i++)
                {
                    string Err = "";
                    Coll.GetActPos((byte)i, ref Err);
                    if (Err != "")
                    {
                        ((UITextBox)(Controls.Find("textBoxActPos" + i.ToString(), true)[0])).Text = Err;
                    }
                }
            }
            else
            {
                button_connect.Text = "连接";
            }
        }

        private void Form_ElectricCollimator_Load(object sender, EventArgs e)
        {
            timer1.Interval = 1000;
            timer1.Start();
        }

        private void buttonGo_Click(object sender, EventArgs e)
        {
            float WDV;
            int channel = Convert.ToInt32(combox_ChannelNo.SelectedIndex.ToString()) + 1;
            if (CheckBox_IsInfinity.Checked)
            {
                string S = "";
                if (Coll.MoveInf(ref S))
                {
                    MessageBox.Show("设置无穷远成功");
                    if (saveDis != null)
                    {
                        saveDis(double.MaxValue);
                    }
                   // OtherDevices.otherDevicesData.distance = double.MaxValue;

                }
                else
                {
                    MessageBox.Show("设置无穷远失败");
                }
            }
            else if (float.TryParse(text_Simulation.Text, out WDV))
            {
                if (WDV < 0.5f)
                {
                    MessageBox.Show("模拟距离需大于0.5m");
                    return;
                }
                else
                {
                    string S = "";
                    if (Coll.MoveDistance(WDV, ref S))
                    {
                        MessageBox.Show($"模拟距离{WDV} 设置成功");
                      
                        if (saveDis != null)
                        {
                            saveDis(WDV);
                        }
                    }
                    else
                    {
                        MessageBox.Show($"模拟距离{WDV} 设置失败");
                    }
                }
             
            }
            else
            {
                MessageBox.Show("输入不合规");
                return;
            }
        }

        private void btn_SetLight_Click(object sender, EventArgs e)
        {
            int channel = combox_ChannelNo.SelectedIndex + 1;
            string s_光源种类 = cb_光源种类.Text;
            string Err = "";
            if (channel < 0)
            {
                MessageBox.Show("请选择通道");
                return;
            }

            int s;
            if (int.TryParse(text_Light.Text, out s))
            {
                if (s < 0)
                {
                    MessageBox.Show("亮度值输入不合规");
                    return;
                }
            }
            else
            {
                MessageBox.Show("亮度值输入不合规");
                return;
            }
            if (s_光源种类 != "白光")
            {
                s_光源种类 = "红外";
            }
            Coll.SetLight(channel, s, ref Err, s_光源种类);
         
            if (Err.Contains("成功"))
            {
                MessageBox.Show(Err);
          
                if (saveVal != null)
                {
                    saveVal(channel, s, s_光源种类);
                }
            }
        
        }
    }
}
